using System;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
    public class DMPropertyPropertyTypeSameAsBOLookupListClassIDType : IBusinessObjectRule
    {
        private DMProperty _dmProperty;

//        public DMPropertyPropertyTypeSameAsBOLookupListClassIDType(DMProperty dmProperty)
//        {
//            if (dmProperty == null) throw new ArgumentNullException("dmProperty");
//            _dmProperty = dmProperty;
//        }

        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _dmProperty = (DMProperty) bo;
            DMLookupListBusinessObject lookupList = _dmProperty.LookupList as DMLookupListBusinessObject;
            if (lookupList == null) return true;
            if (_dmProperty.PropertyType == null) return true;
            DMClass dmClass = lookupList.DMClass;
            if (dmClass == null) return true;
            DMObjectIdentity dmObjectIdentity = dmClass.GetObjectID();
            if (dmObjectIdentity == null) return true;
            if (dmObjectIdentity.IsCompositeKey) return false;
            if (!dmObjectIdentity.HasProperties) return true;
            if (dmObjectIdentity.Properties[0].PropertyTypeString == _dmProperty.PropertyTypeString) return true;
            return false;
        }

        public string Name
        {
            get { return "DMPropertyPropertyTypeSameAsBOLookupListClassIDType"; }
        }

        public string Message
        {
            get
            {
                DMLookupListBusinessObject lookupList = (DMLookupListBusinessObject)_dmProperty.LookupList;
                return "The property '" + _dmProperty +
                       "' on Class '" + _dmProperty.Class +
                       "' and the ID of its Business Object Lookup Class '" + lookupList.DMClass +
                       "' do not have the same property type.";
            }
        }

        public ErrorLevel ErrorLevel
        {
            get { return ErrorLevel.Warning; }
        }
    }
}